angular.module('module.order', [
        "ngCookies"
    ])
    .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
        $stateProvider
        //订单
            .state('botMenu.ordercenter', {
                url: '/ordercenter',
                templateUrl: 'view/order/ordercenter.html',
                controller: 'OrderCenterController'
            })
            .state('botMenu.orderdetail', {
                url: '/orderdetail',
                templateUrl: 'view/order/orderdetail.html',
                controller: 'OrderDetailController'
            })
        ;
    }])

    .controller('OrderCenterController', ['$scope', '$state', '$cookieStore', 'RegisterService', 'OrderService', 'MarketService', 'LoginService','advertisementService', function ($scope, $state, $cookieStore, RegisterService, OrderService, MarketService, LoginService,advertisementService) {
        //获取账号信息
        if ($cookieStore.get('_user') != undefined) {
            var data = $cookieStore.get('_user');
            var cid = data.userid;
            OrderService.getOrderByCid(cid).$promise.then(function (orderList) {
                $scope.orderList = orderList;
                console.log(orderList);
                market_privilege(orderList, 0);
                adv_privilege(orderList, 0);
                //分页
                var url = window.location.href;
                var page = {
                    name: "订单中心",
                    url: url
                };
                $scope.pageList = LoginService.SavePageList(page);
                $scope.currentPage = 1;
                $scope.pageSize = 8;
            });
        } else {
            $state.go("login");
        }
        //通过递归的方法获取超市信息
        function market_privilege(list, i) {
            if (i >= list.length) {
                return;
            }
            var item = list[i];
            OrderService.getMarketByMarketid(item.marketid).$promise.then(function (market) {
                item.market = market;
                market_privilege(list, i + 1);
            }, function () {
            });
        }
        //通过递归的方法获取广告信息
        function adv_privilege(list, i) {
            if (i >= list.length) {
                return;
            }
            var item = list[i];
            advertisementService.getAdvByAdvId(item.aid).$promise.then(function (adv) {
                item.adv = adv;
                adv_privilege(list, i + 1);
            }, function () {
            });
        }
        $scope.order = {};
        $scope.statusFilter = function(status){
            if(status!=undefined){
                console.log(status);
                $scope.order.orderstatus = status;
            }else{
                $scope.order.orderstatus = "";
            }
        }
    }])
    .controller('OrderDetailController', ['$scope', function ($scope) {

    }])